#ifndef BASE_SYSTEM_LOG_LOGGER_INCLUDE_FIXED_BUFFER_H
#define BASE_SYSTEM_LOG_LOGGER_INCLUDE_FIXED_BUFFER_H

#include <cstddef>
#include <cstring>
#include <memory>
#include <string>
#include <type_traits>
#include "system_log_config.h"

namespace Base {
namespace SystemLog {
template <size_t CapacitySize = DEFAULT_BUFFER_SIZE>
class FixedBuffer {
public:
    FixedBuffer() : data_(std::make_unique<char[]>(CapacitySize)), cur_(data_.get()) {}
    ~FixedBuffer() = default;

    // 禁用拷贝构造和拷贝赋值
    FixedBuffer(const FixedBuffer&) = delete;
    FixedBuffer& operator=(const FixedBuffer&) = delete;
    
    // 启用移动构造和移动赋值
    FixedBuffer(FixedBuffer&&) = default;
    FixedBuffer& operator=(FixedBuffer&&) = default;

    void Append(const char* buf, size_t len) {
        if (Available() > len) {
            std::memcpy(cur_, buf, len);
            cur_ += len;
        }
    }

    template <typename T>
    void AppendValue(const T &value) {
        static_assert(std::is_arithmetic<T>::value, "only arithmetic types are supported");
        if (Available() >= sizeof(T)) {
            *reinterpret_cast<T*>(cur_) = value;
            cur_ += sizeof(T);
        }
    }

    void Reset() {
        cur_ = data_.get();
    }

    size_t Length() const {
        return static_cast<size_t>(cur_ - data_.get());
    }

    size_t Capacity() const {
        return CapacitySize;
    }

    size_t Available() const {
        return Capacity() - Length();
    }

    const char* Data() const {
        return data_.get();
    }

    char* Current() {
        return cur_;
    }

    void Add(size_t len) {
        cur_ += len;
    }

    std::string ToString() const {
        return std::string(data_.get(), Length());
    }

private:
    std::unique_ptr<char[]> data_;
    char* cur_;
};

extern template class FixedBuffer<DEFAULT_BUFFER_SIZE>;
} // namespace SystemLog
} // namespace Base

#endif // BASE_SYSTEM_LOG_LOGGER_INCLUDE_FIXED_BUFFER_H